#Include <FindText>
#Include ..\const\LightningBuyConst.ahk
#Include ..\const\LightningSellConst.ahk
#Include ..\utils\KeyboardUtils.ahk
#Include ..\utils\HwndUtils.ahk
#Include ..\utils\TipUtils.ahk
#Include ..\const\ClientConstant.ahk
#Include ..\const\KeyConst.ahk
#Include ..\service_api\TradeService.ahk
#Include ..\utils\OcrUtils.ahk

; 买入交易的实现类
class BuyTradeServiceImpl  extends TradeService{
    findLightningWindow(){
        ; 激活应用的窗口
        HwndUtils.activateWindow()
        ; 查询闪电买入的窗口
        buy_window_text := LightningBuyConst.LIGHTNING_BUY_WINDOW_TITLE
        ok:=FindText(&X:="wait", &Y:=3, 0,0,0,0,0,0,buy_window_text)  ; 等待3秒等图像出现\
        if(ok){
            TipUtils.tip("找到了闪电买入的窗口")
            return
        }

        ; 如果没有找到闪电买入的窗口,反而存在了闪电卖出的窗口,则需要先关闭闪电卖出的窗口
        sell_window_text:= LightningSellConst.LIGHTNING_SELL_WINDOW_TITLE
        ok:=FindText(&X:="wait", &Y:=3, 0,0,0,0,0,0,sell_window_text)
        if(ok){
            TipUtils.tip("找到了闪电卖出的窗口,开始关闭闪电卖出窗口")
            FindText().Click(X, Y, "L")
            KeyboardUtils.esc()
            TipUtils.tip("关闭完成")
        }
        ; 激活应用窗口
        HwndUtils.activateWindow()
        ; 输入闪电买入的 快捷键 21
        KeyboardUtils.selectAll()
        KeyboardUtils.input(KeyConst.LIGHTING_BUY_KEY)
        KeyboardUtils.enter()
        t1:=A_TickCount, Text:=X:=Y:=""
        ; 21   闪电买入(普通或担保) 交易 字符的查找
        Text := LightningBuyConst.FIND_SD_BUY_DESC
        if (ok:=FindText(&X, &Y, 872-150000, 502-150000, 872+150000, 502+150000, 0, 0, Text))
        {
           KeyboardUtils.enter()
        }
    }
;D:\Software\anaconda3\envs\env-wealth-center-auto-3.6\python.exe
;D:/java_project/wealth-center-auto-buy/startScreenShot.py 000100 894 554 1017 577
     findPriceAmount(position){
        ; 初始化剪切板
        input_position := ""
        while input_position != position {
            Text := LightningBuyConst.BUY_AMOUNT
                    ok:=FindText(&X:="wait", &Y:=ClientConstant.SCREEN_QUERY_TIME, 0,0,0,0,0,0,Text)  ; 等待3秒等图像出现\
                    if(ok){
                        FindText().Click(X+ClientConstant.X_OFFSET, Y, "L")
                        KeyboardUtils.LButtonDown()
                        MouseMove X+ClientConstant.X_OFFSET - 300 , 100
                        KeyboardUtils.copy()
                        KeyboardUtils.LButtonUp()
                        KeyboardUtils.input(position)
                        KeyboardUtils.selectAll()
                        KeyboardUtils.copy()

                        x1 := X + LightningConst.POSITION_OFFSET_X1
                        y1 := Y + LightningConst.POSITION_OFFSET_Y1

                        x2 := X + LightningConst.POSITION_OFFSET_X2
                        y2 := Y + LightningConst.POSITION_OFFSET_Y2
                        ; 构建命令行
                        command := ClientConstant.PYTHON_EXE " " ClientConstant.PYTHON_SCRIPT_PATH " " position " " x1 " " y1 " " x2 " " y2
                        ; 调用 Python 脚本并捕获输出

                        ; 检查是否有参数传递
                        MsgBox command
                        imageName := ClientConstant.getPositionNumberPath(position)
                        input_position := OcrUtils.ocrFromFile(imageName)
                        MsgBox input_position
                    }

        }
    }

     findOperationButton(){
        Text := LightningBuyConst.BUY_BUTTON_TEXT
        ok:=FindText(&X:="wait", &Y:=ClientConstant.SCREEN_QUERY_TIME, 0,0,0,0,0,0,Text)  ; 等待3秒等图像出现\
        if(ok){
            Sleep 1000
            FindText().Click(X, Y, "L")
        }
    }

    findSucceTip(){
        Text:=LightningBuyConst.BUY_SUCCESS_TIP_TEXT
        ok:=FindText(&X:="wait", &Y:=ClientConstant.SCREEN_QUERY_TIME, 0,0,0,0,0,0,Text)
        if(ok){
            TipUtils.tip("卖出完成")
            KeyboardUtils.enter()
        }
    }
}